package com.td.framework.utils;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * 
 * Title: 日志工具 
 * Description:用来输出不同日志 
 * Copyright: Copyright (c) 2008 Company
 * 北京水软科技有限公司
 * 
 * @author　FLT003
 * @version 1.0
 */
public class LogUtils {

	private static final Level INFO = Level.INFO;// 信息

	private static final Level DEBUG = Level.DEBUG;// 测试

	private static final Level WARN = Level.WARN;// 警告

	private static final Level ERROR = Level.ERROR;// 错误

	private static final Level ALARM = Level.FATAL;// 严重错误

	public static Logger logger = Logger.getLogger("com.fulitong");// Loger对象

	/**
	 * 按照日志配置信息写日志
	 * 
	 * @param logMessage
	 * @param errorLevel
	 * @param e
	 */
	private static void printLog(String logMessage, Level errorLevel,
			Throwable e) {
		try {
			StringBuffer sbLog = new StringBuffer();
			sbLog.append("【线程编号：");
			sbLog.append(Thread.currentThread().getName());
			sbLog.append("】");
			if (errorLevel == null || errorLevel == INFO) {
				errorLevel = INFO;
				sbLog.append("【信息】");
			} else if (errorLevel == DEBUG) {
				sbLog.append("【调试】");
			} else if (errorLevel == WARN) {
				sbLog.append("【警告】");
			} else if (errorLevel == ERROR) {
				sbLog.append("【错误】");
			} else if (errorLevel == ALARM) {
				sbLog.append("【严重错误】");
			}
			sbLog.append(logMessage);
			logger.log(errorLevel, sbLog.toString(), e);
		} catch (Exception ex) {
			logger.error("记录日志时发生以下错误：" + ex.getMessage());
		}
	}

	/**
	 * 按照日志配置信息写日志
	 * 
	 * @param logMessage
	 * @param errorLevel
	 */
	private static void printLog(String logMessage, Level errorLevel) {
		printLog(logMessage, errorLevel, null);
	}

	/**
	 * 错误日志
	 * 
	 * @param object
	 */
	public static void error(Object object) {
		printLog(object.toString(), ERROR);
	}

	/**
	 * 错误日志
	 * 
	 * @param object
	 * @param t
	 */
	public static void error(Object object, Throwable t) {
		printLog(object.toString(), ERROR, t);
	}

	/**
	 * 调试错误日志
	 * 
	 * @param object
	 */
	public static void debug(Object object) {
		printLog(object.toString(), DEBUG);
	}

	/**
	 * 调试错误日志
	 * 
	 * @param object
	 * @param t
	 */
	public static void debug(Object object, Throwable t) {
		printLog(object.toString(), DEBUG, t);
	}

	/**
	 * 信息日志
	 * 
	 * @param object
	 */
	public static void info(Object object) {
		printLog(object.toString(), INFO);
	}

	/**
	 * 信息日志
	 * 
	 * @param object
	 * @param t
	 */
	public static void info(Object object, Throwable t) {
		printLog(object.toString(), INFO, t);
	}

	/**
	 * 警告日志
	 * 
	 * @param object
	 */
	public static void warn(Object object) {
		printLog(object.toString(), WARN);
	}

	/**
	 * 警告日志
	 * 
	 * @param object
	 * @param t
	 */
	public static void warn(Object object, Throwable t) {
		printLog(object.toString(), WARN, t);
	}

	/**
	 * 提醒日志
	 * 
	 * @param object
	 */
	public static void alerm(Object object) {
		printLog(object.toString(), ALARM);
	}

	/**
	 * 提醒日志
	 * 
	 * @param object
	 * @param t
	 */
	public static void alerm(Object object, Throwable t) {
		printLog(object.toString(), ALARM, t);
	}

}
